package cn.iocoder.yudao.module.design.service.projectmanager;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.design.controller.admin.projectmanager.vo.ProjectManagerCreateReqVO;
import cn.iocoder.yudao.module.design.controller.admin.projectmanager.vo.ProjectManagerExportReqVO;
import cn.iocoder.yudao.module.design.controller.admin.projectmanager.vo.ProjectManagerPageReqVO;
import cn.iocoder.yudao.module.design.controller.admin.projectmanager.vo.ProjectManagerUpdateReqVO;
import cn.iocoder.yudao.module.design.convert.projectmanager.ProjectManagerConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.projectmanager.ProjectManagerDO;
import cn.iocoder.yudao.module.design.dal.mysql.projectmanager.ProjectManagerMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.PROJECT_MANAGER_NOT_EXISTS;

/**
 * 认证项目经理 Service 实现类
 *
 * @author czw
 */
@Service
@Validated
public class ProjectManagerServiceImpl implements ProjectManagerService {

    @Resource
    private ProjectManagerMapper projectManagerMapper;

    @Override
    public Long createProjectManager(ProjectManagerCreateReqVO createReqVO) {
        // 插入
        ProjectManagerDO projectManager = ProjectManagerConvert.INSTANCE.convert(createReqVO);
        projectManagerMapper.insert(projectManager);
        // 返回
        return projectManager.getId();
    }

    @Override
    public void updateProjectManager(ProjectManagerUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateProjectManagerExists(updateReqVO.getId());
        // 更新
        ProjectManagerDO updateObj = ProjectManagerConvert.INSTANCE.convert(updateReqVO);
        projectManagerMapper.updateById(updateObj);
    }

    @Override
    public void deleteProjectManager(Long id) {
        // 校验存在
        this.validateProjectManagerExists(id);
        // 删除
        projectManagerMapper.deleteById(id);
    }

    private void validateProjectManagerExists(Long id) {
        if (projectManagerMapper.selectById(id) == null) {
            throw exception(PROJECT_MANAGER_NOT_EXISTS);
        }
    }

    @Override
    public ProjectManagerDO getProjectManager(Long id) {
        return projectManagerMapper.selectById(id);
    }

    @Override
    public List<ProjectManagerDO> getProjectManagerList(Collection<Long> ids) {
        return projectManagerMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProjectManagerDO> getProjectManagerPage(ProjectManagerPageReqVO pageReqVO) {
        return projectManagerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProjectManagerDO> getProjectManagerList(ProjectManagerExportReqVO exportReqVO) {
        return projectManagerMapper.selectList(exportReqVO);
    }

}
